<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // function createWorker(){
        //     const workerString = `
        //         function emit(message){
        //             console.log(message);
        //         }
        //     `;
        //     const workerStringBlob = new Blob([workerString]);
        //     const url = URL.createObjectURL(workerStringBlob);
        //     const p = document.createElement('script');
        //     p.src = url;
        //     document.body.appendChild(p);
        // }
        // createWorker();
        function createWorker(cb){
            const workerString = cb.toString();
            // console.log(workerString)
            const workerStringBlob = new Blob([workerString]);
            const url = URL.createObjectURL(workerStringBlob);
            return new Worker(url);
        }

        console.log(createWorker(function(){
            self.onmessage = function (event) {
                var num = event.data;

                var T = setInterval(function () {
                    self.postMessage(--num);
                    if (num <= 0) {
                        clearInterval(T);
                        self.close();
                        console.log('clearInterval & worker closed');
                    }
                }, 1000);
            }
        }))
    </script>
</body>
</html>